﻿#region

using System.Web.Mvc;

#endregion

namespace HouseHoldBudget.Framework.Mvc.Controllers
{
    /// <summary>
    ///   If form name exists, then specified "actionParameterName" will be set to "true"
    /// </summary>
    public class ParameterBasedOnFormNameAttribute : FilterAttribute, IActionFilter
    {
        #region Readonly & Static Fields

        private readonly string _actionParameterName;
        private readonly string _name;

        #endregion

        #region Constructors

        public ParameterBasedOnFormNameAttribute(string name, string actionParameterName)
        {
            _name = name;
            _actionParameterName = actionParameterName;
        }

        #endregion

        #region IActionFilter Members

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var formValue = filterContext.RequestContext.HttpContext.Request.Form[_name];
            filterContext.ActionParameters[_actionParameterName] = !string.IsNullOrEmpty(formValue);
        }

        #endregion
    }
}